package com.bd.scm.module.user.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bd.offices.excel.ExcelUtil;
import com.bd.scm.domain.dto.aggrid.AgGridRequest;
import com.bd.scm.domain.dto.aggrid.AgGridResponse;
import com.bd.scm.domain.dto.user.PersonnelFilesDto;
import com.bd.scm.domain.models.user.TransferUser;
import com.bd.scm.domain.models.user.UserVacation;
import com.bd.scm.module.common.BaseController;
import com.bd.scm.module.enums.ExceptionEmun;
import com.bd.scm.module.user.service.TransferUserService;
import com.bd.scm.module.user.service.UserVacationService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.vo.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户前端控制器
 * User: _Cps
 * Date: 2019.08.28 16:44
 */
@Slf4j
@Api("临时借调接口")
@RestController
@RequestMapping("/api/transferUse")
public class TransferUserController extends BaseController {

    @Autowired
    private TransferUserService transferUserService;

    /**
     * 分页获取临时借调信息.
     *
     * @param request 条件
     * @return 人员信息集合
     */
    @RequestMapping("transferUserList")
    @ResponseBody
    public Result transferUserList(@RequestBody AgGridRequest request) {
        Result<AgGridResponse> result = new Result<AgGridResponse>();
        //之后要根据传值，
        TransferUser transferUser = JSON.parseObject(JSON.toJSONString(request.getData()),
                TransferUser.class);
        Page<TransferUser> page = getPage(request);
        ;
        IPage<TransferUser> pageList = this.transferUserService.transferUserList(page, transferUser);
        AgGridResponse<TransferUser> response = getResponse(request, pageList, page);
        result.setSuccess(true);
        result.setResult(response);
        return result;
    }
    /**
     * 临时借调
     *
     * @param transferUser 条件
     * @return
     */
    @RequestMapping("/addLoanInfo")
    @ResponseBody
    public Result addLoanInfo(@RequestBody TransferUser transferUser,HttpServletRequest request ) throws UnknownHostException {
        String clientIp = InetAddress.getLocalHost().getHostAddress();
        JSONObject user = (JSONObject) request.getSession().getAttribute("user");
        transferUser.setGlyNo(user.getString("userSerial"));
        int flag = this.transferUserService.addLoanInfo(transferUser, clientIp);
        if (flag > 0) {
            return ResultUtil.ok();
        } else {
            return ResultUtil.error(ExceptionEmun.USER_ERROR_LOAN);
        }
    }

    /**
     * 人员导出
     *
     * @param serchTxt
     * @param response
     * @return
     * @throws IOException
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
    @RequestMapping(value = "transferUserExport")
    public Result transferUserExport(String serchTxt, String depSerial,String userSerial, HttpServletResponse response) throws IOException, InstantiationException, IllegalAccessException {
        TransferUser transferUser = new TransferUser();
        transferUser.setSerchTxt(serchTxt);
        transferUser.setDepSerial(depSerial);
        transferUser.setUserSerial(userSerial);
        List<TransferUser> doorDefinition = this.transferUserService.transferUserList2(transferUser);
        ExcelUtil.ExportExcel("导出人员调动列表" , response, doorDefinition, TransferUser.class);
        return ResultUtil.ok();
    }
}
